/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of CustomerStore.
	/// </summary>
	public class CustomerStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		private JsonDatabase db;
		public void PersistCustomers(List<Dictionary<string,object>> Customers)
		{
			db.Write<List<Dictionary<string,object>>>(Customers,_site.Name);
		}

		public void PersistCustomer(Dictionary<string,object> Customer)
		{
			db.Write<Dictionary<string,object>>(Customer,Customer["customers_id"].ToString());
		}
		
		public CustomerStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/Customers");
			_site = site;
			_client = client;
		}
		
		public List<Dictionary<string,object>> GetCustomers()
		{
			List<Dictionary<string,object>> Customers = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_customers&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Customer = (Dictionary<string,object>)recs[i];
						Customer["address_books"] = GetCustomerAdresses(Customer["customers_id"].ToString());
						Customer["store_credits"] = GetCustomerCredits(Customer["customers_id"].ToString());
						Customer["wishlists"] = GetCustomerWishlists(Customer["customers_id"].ToString());
						Customers.Add(Customer);
					}									
				}
			}			
			
			return Customers;
		}
		
		private List<Dictionary<string,object>> GetCustomerWishlists(string CustomerId)
		{
			List<Dictionary<string,object>> Wishlists = new List<Dictionary<string,object>>();
			
			string url = _site.Url + "/admin/json.php?action=list_wishlists&customers_id=" + CustomerId + "&module=customers";
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Wishlist = (Dictionary<string,object>)recs[i];
						Wishlists.Add(Wishlist);												
					}									
				}
			}			
			
			return Wishlists;
		}
		
		private List<Dictionary<string,object>> GetCustomerCredits(string CustomerId)
		{
			List<Dictionary<string,object>> Credits = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_store_credits&customers_id=" + CustomerId + "&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Credit = (Dictionary<string,object>)recs[i];
						Credits.Add(Credit);												
					}									
				}
			}			
			
			return Credits;
		}
		
		private List<Dictionary<string,object>> GetCustomerAdresses(string CustomerId)
		{
			List<Dictionary<string,object>> Adresses = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_address_books&customers_id=" + CustomerId + "&module=customers");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> Adresse = (Dictionary<string,object>)recs[i];
						Adresses.Add(Adresse);												
					}									
				}
			}			
			
			return Adresses;
		}
	}
}
